<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>可以判断用户离开或是进来了</title>
</head>

<body>

</body>
<script>
  "use strict";
  document.addEventListener("visibilitychange", async () => {
    document.title = document.hidden ? "用户离开了" : "用户回来了";
  });
</script>

</html>